<?php
class Email
{
	function email(){
		
	}
	
	/**
	*	Funcion encargada de insertar un nuevo registro de email.
	*	@sutor: JSL
	*	@date: 03/02/2012
	*/
	function addEmail($data){
		$sql = "INSERT INTO email (email_code, use_code, email_subject, email_content, email_to, email_date_create ) VALUES 
				(NULL, ".$data->use_code.", '".$data->email_subject."', '".$data->email_content."', '".$data->email_to."', '".date("Y--m-d H:i:s")."' )";
		
		if ( mysql_query ($sql) )
			return mysql_insert_id ();
		else
			return false;
	}
	
	/**
	*	Funcion encargada de insertar un nuevo registro de attachment para un email.
	*	@sutor: JSL
	*	@date: 03/02/2012
	*/
	function addAttachment($data){
		$sql = "INSERT INTO attachment (attach_code, email_code, attach_name, attach_rename) VALUES 
				(NULL, ".$data->email_code.", '".$data->attach_name."', '".$data->attach_rename."')";
		
		if ( mysql_query ($sql) )
			return true;
		else
			return false;
	}
	
	/**
	*	Funcion encargada de listar los emails enviados.
	*	@sutor: JSL
	*	@date: 03/02/2012
	*/
	function listEmail($search = '', $order_by = 0, $init = 0, $amount = 0){
		
		$where = '';
		if ( $search != '' )
			$where = " WHERE (use_name like '%".$search."%' OR use_lastname like '%".$search."%' OR email_subject like '%".$search."%' OR use_email like '%".$search."%'
						 OR email_to like '%".$search."%')";
		
		$sql = "SELECT e.email_code, e.use_code, e.email_subject, e.email_content, e.email_to, e.email_date_create, u.use_name, u.use_lastname, u.use_email FROM email e 
				JOIN user u ON e.use_code=u.use_code ".$where." ".$order_by;
		
		if ( ! ( $init == 0 || $amount == 0) )
			$sql .= " LIMIT ".$init.", ".$amount.";";
		
		if ( $result = mysql_query ($sql) ){
			$i = 0;
			while ( $row = mysql_fetch_array ($result) ){
				$data[$i]->email_code = $row['email_code'];
				$data[$i]->use_code = $row['use_code'];
				$data[$i]->email_subject = $row['email_subject'];
				$data[$i]->email_content = $row['email_content'];
				$data[$i]->email_to = $row['email_to'];
				$data[$i]->email_date_create = $row['email_date_create'];
				$data[$i]->use_name = $row['use_name'];
				$data[$i]->use_lastname = $row['use_lastname'];
				$data[$i]->use_email = $row['use_email'];
				
				$data[$i]->attachments = $this->getAttachemts($data[$i]->email_code);
				
				$i++;
			}
			
			return $data;
		}
		else
			return false;
	}
	
	/**
	*	Funcion responsable de contar la cantidad de emails enviados por la aplicacion.
	*	@autor: JSL
	*	@date: 03/02/2012
	*/
	function countEmail($search = ''){
		$where = '';
		if ( $search != '' )
			$where = " WHERE (use_name like '%".$search."%' OR use_lastname like '%".$search."%' OR email_subject like '%".$search."%' OR use_email like '%".$search."%'
						 OR email_to like '%".$search."%')";
		
		$sql = "SELECT COUNT(*) As cant FROM email e JOIN user u ON e.use_code=u.use_code ".$where.";";
		
		if ( $result = mysql_query ($sql) ){
			if ( $row = mysql_fetch_array ( $result ) ){
				return $row["cant"];
			}
			else
				return false;
		}
		else
			return false;
	}
	
	/**
	*	Funcion encargada de obtener los attachment que tiene un email.
	*	@autor: JSL
	*	@date: 03/02/2012
	*/
	function getAttachemts($email_code){
		$sql = "SELECT a.attach_code, a.attach_name, a.attach_rename FROM attachment a WHERE a.email_code=".$email_code.";";
		if ( $result = mysql_query ($sql) ){
			$i = 0;
			while ( $row = mysql_fetch_array ($result) ){
				$data[$i]->attach_code = $row['attach_code'];
				$data[$i]->attach_name = $row['attach_name'];
				$data[$i]->attach_rename = $row['attach_rename'];
				
				$i++;
			}
			
			return $data;
		}
		else
			return false;
	}
	
}
?>